/* eslint no-undef: off */
import Vue from 'vue'
import { storage } from './index.js'
import browser from './browser.js'
/**
 * 2019年11月8日20:21:07
 * change: 游易赚修改
 * author: niezhangxun
 */

/* ========== Android 原生通信 ========== */

/**
 * 通用Toast
 * @param {*} tips
 * @param {*} isLong
 */
const showToast = (tips, isLong = 0) => {
  if (browser.app()) {
    return new Promise((resolve) => {
      Vue.prototype.$JsBridge.callHandler('onShowToast', {
        msg: tips,
        time: isLong
      });
      setTimeout(() => {
        resolve()
      }, isLong === 0 ? 1500 : 3000);
    })
  } else {
    return new Promise((resolve) => {
      $toast.show(tips, isLong === 0 ? 1500 : 3000).then(() => {
        resolve()
      })
    })
  }
}
Vue.prototype.$showToast = showToast


/**
 * 复制功能
 * @param {*} content
 */
const textCopy = content => {
  if (browser.app()) {
    return new Promise((resolve) => {
      Vue.prototype.$JsBridge.callHandler(
        'onTextCopy', {
          msg: content
        },
        responseData => {
          resolve(Number(responseData))
        }
      );
    });
  } else {
    return new Promise((resolve) => {
      resolve()
    });
  }
}
Vue.prototype.$textCopy = textCopy

/**
 * 打开QQ
 */
const openQQ = (url) => {
  if (browser.app()) {
    return new Promise((resolve) => {
      Vue.prototype.$JsBridge.callHandler(
        'onOpenQQ', { url: url },
        responseData => {
          resolve(Number(responseData))
        }
      );
    });
  } else {
    return new Promise((resolve) => {
      resolve()
    });
  }
}
Vue.prototype.$openQQ = openQQ

/**
 * 打开微信
 */
const openWechat = () => {
  if (browser.app()) {
    return new Promise((resolve) => {
      Vue.prototype.$JsBridge.callHandler(
        'onOpenWechat', {},
        responseData => {
          resolve(Number(responseData))
        }
      );
    });
  } else {
    return new Promise((resolve) => {
      resolve()
    });
  }
}
Vue.prototype.$openWechat = openWechat

/**
 * 系统分享
 */
const onShare = () => {
  if (browser.app()) {
    return new Promise((resolve) => {
      Vue.prototype.$JsBridge.callHandler(
        'onShare', {},
        responseData => {
          resolve(responseData)
        }
      );
    });
  } else {
    return new Promise((resolve) => {
      resolve()
    });
  }
}
Vue.prototype.$onShare = onShare

/**
 * 读取缓存
 * @param {*} key
 */
const GetStorage = (key) => {
  if (browser.android() && browser.app()) {
    return new Promise((resolve) => {
      Vue.prototype.$JsBridge.callHandler(
        'natGetFromPrefer', { key: key },
        responseData => {
          let res = JSON.parse(responseData)
          resolve(res.value)
        }
      );
    });
  } else {
    return new Promise((resolve) => {
      storage.get(key)
      resolve(storage.get(key))
    });
  }
}
Vue.prototype.$GetStorage = GetStorage

/**
 * 设置缓存
 * @param {*} key
 * @param {*} value
 */
const SetStorage = (key, value) => {
  if (browser.android() && browser.app()) {
    return new Promise((resolve) => {
      Vue.prototype.$JsBridge.callHandler(
        'natSaveToPrefer', { key: key, value: value },
        responseData => {
          let res = JSON.parse(responseData)
          resolve(res)
        }
      );
    });
  } else {
    return new Promise((resolve) => {
      storage.set(key, value)
      resolve()
    });
  }
}
Vue.prototype.$SetStorage = SetStorage
